﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System.Windows;
using Microsoft.Phone.Controls;
using YinYueTai.HtmlParser;
using System;
using System.IO;
using GalaSoft.MvvmLight.Command;
using System.Collections;
using System.Collections.Generic;
using YinYueTai.Model;
using System.Windows.Controls;

namespace YinYueTai.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class VideoViewModel : ViewModelBase
    {

        public static MvDetail mv;

        private WebBrowser player;
        //private MvDetail mv;

        public MvDetail Mv
        {
            get
            {
                return mv;
            }
            set
            {
                mv = value;
                RaisePropertyChanged("Mv");
            }
        }


        /// <summary>
        /// The <see cref="Orientation" /> property's name.
        /// </summary>
        public const string OrientationPropertyName = "Orientation";

        private PageOrientation _orientaition = PageOrientation.Portrait ;

        /// <summary>
        /// Sets and gets the Orientation property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public PageOrientation Orientation
        {
            get
            {
                return _orientaition;
            }

            set
            {
                if (_orientaition == value)
                {
                    return;
                }

                _orientaition = value;
                RaisePropertyChanged(OrientationPropertyName);
                RaisePropertyChanged("IsEn");
            }
        }

        public ScrollBarVisibility IsEn
        {
            get
            {
                return (ScrollBarVisibility)((int)(_orientaition+1) % 2);
            }
        }
        /// <summary>
        /// Initializes a new instance of the VideoViewModel class.
        /// </summary>
        public VideoViewModel()
        {
            //Messenger.Default.Register<String>(this, "OK", e => Play(e));
            Messenger.Default.Register<WebBrowser>(this, "SETPLAYER", w =>
            {
                player = w;
                Orientation = PageOrientation.Portrait;
            });
            
            if (mv != null)
            {
                Video v = new Video(mv.ID, mm => Mv = mm);
                v.GetInfo(Mv);
            }
            //Messenger.Default.Send(new NotificationMessage("RUN"));
            //Messenger.Default.Unregister(Msg.Instance);

        }


        private RelayCommand _loadMV;

        /// <summary>
        /// Gets the LoadMV.
        /// </summary>
        public RelayCommand LoadMV
        {
            get
            {
                return _loadMV
                    ?? (_loadMV = new RelayCommand(
                                          () =>
                                          {
                                              if (mv.VidoeSrc==null)
                                              {
                                                  Video v = new Video(mv.ID, mm => Mv = mm);
                                                  v.GetInfo(Mv);
                                                  
                                              }
                                          }));
            }
        }

        private RelayCommand<IDictionary<string, string>> _setUrl;

        /// <summary>
        /// Gets the MyCommand.
        /// </summary>
        public RelayCommand<IDictionary<string, string>> SetUrl
        {
            get
            {
                return _setUrl
                    ?? (_setUrl = new RelayCommand<IDictionary<string, string>>(
                                          p =>
                                          {
                                              MessageBox.Show(p["Url"]);
                                          }));
            }
        }

        private RelayCommand _playCommand;

        /// <summary>
        /// Gets the PlayCommand.
        /// </summary>
        public RelayCommand PlayCommand
        {
            get
            {
                return _playCommand
                    ?? (_playCommand = new RelayCommand(
                                          () =>
                                          {
                                              Play();
                                          }));
            }
        }

        private void Play()
        {
            if (mv.VidoeSrc == null)
                return;
            var x = App.GetResourceStream(new Uri("VideoLoader.html", UriKind.Relative));

            string t;
            using (StreamReader sr = new StreamReader(x.Stream))
                t = sr.ReadToEnd();
            t = t.Replace("{0}", mv.VidoeSrc);
            player.NavigateToString(t);
        }
       
    }
}